package com.yxw.yxnet_cd_center.gateway.exception;

import com.yxw.yxnet_cd_center.common.enums.CommonErrorEnum;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import com.yxw.yxnet_cd_center.gateway.utils.IpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.springframework.web.server.ResponseStatusException;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 */
@Slf4j
public abstract class AbstractExceptionHandler {
    private static final int DEFAULT_ERROR_CODE = 500;

    protected String formatMessage(Throwable ex) {
        String errorMessage = null;
        if (ex instanceof NotFoundException) {
            String reason = ((NotFoundException) ex).getReason();
            errorMessage = reason;
        } else if (ex instanceof ResponseStatusException) {
            ResponseStatusException responseStatusException = (ResponseStatusException) ex;
            errorMessage = responseStatusException.getMessage();
        } else {
            errorMessage = ex.getMessage();
        }
        return errorMessage;
    }

    protected Map<String, Object> buildErrorMap(String errorMessage) {
        Map<String, Object> resMap = new HashMap<>(3);
        resMap.put("code", DEFAULT_ERROR_CODE);
        resMap.put("msg", errorMessage);
        resMap.put("data", null);
        resMap.put("ok", false);
        return resMap;
    }

    protected BaseResult generateResult(Throwable ex) {
        if (ex instanceof BaseException) {
            BaseException e = (BaseException) ex;
            BaseResult resultIn = new BaseResult<>();
            resultIn.setResultCode(e.getCode());
            resultIn.setResultMsg(e.getDescription());
            resultIn.setPrimaryErrorCode(e.getPrimaryErrorCode());
            resultIn.setPrimaryErrorMsg(e.getPrimaryErrorMsg());
            resultIn.setPrimaryErrorIP(IpUtil.getIp());
            log.error("出现指定错误:{}", ex.getMessage());
            return resultIn;
        } else if (ex instanceof NotFoundException) {
            String reason = ((NotFoundException) ex).getReason();
            BaseResult resultIn = new BaseResult<>();
            resultIn.setResultCode(CommonErrorEnum.UNKNOWN_ERROR.getCode());
            resultIn.setResultMsg(CommonErrorEnum.UNKNOWN_ERROR.getDescription());
            resultIn.setPrimaryErrorMsg(reason);
            resultIn.setPrimaryErrorIP(IpUtil.getIp());
            log.error("出现指定系统错误:{}", ex.getMessage());
            return resultIn;
        } else if (ex instanceof ResponseStatusException) {
            ResponseStatusException responseStatusException = (ResponseStatusException) ex;
            String errorMessage = responseStatusException.getMessage();
            BaseResult resultIn = new BaseResult<>();
            resultIn.setResultCode(CommonErrorEnum.UNKNOWN_ERROR.getCode());
            resultIn.setResultMsg(CommonErrorEnum.UNKNOWN_ERROR.getDescription());
            resultIn.setPrimaryErrorMsg(errorMessage);
            resultIn.setPrimaryErrorIP(IpUtil.getIp());
            log.error("出现指定系统错误:{}", ex.getMessage());
            return resultIn;
        } else {
            BaseResult resultIn = new BaseResult<>();
            resultIn.setResultCode(CommonErrorEnum.UNKNOWN_ERROR.getCode());
            resultIn.setResultMsg(CommonErrorEnum.UNKNOWN_ERROR.getDescription());
            resultIn.setPrimaryErrorMsg(ex.getMessage());
            resultIn.setPrimaryErrorIP(IpUtil.getIp());
            log.error("出现指定系统错误:{}", ex.getMessage());
            return resultIn;
        }
    }
}
